package com.iteaj.network.codec.adapter;

import com.iteaj.network.AbstractMessage;
import com.iteaj.network.codec.IotMessageDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LineBasedFrameDecoder;

public class LineBasedFrameMessageDecoderAdapter extends LineBasedFrameDecoder implements IotMessageDecoder {

    private Class<? extends AbstractMessage> messageClass;

    public LineBasedFrameMessageDecoderAdapter(int maxLength) {
        super(maxLength);
    }

    public LineBasedFrameMessageDecoderAdapter(int maxLength, boolean stripDelimiter, boolean failFast) {
        super(maxLength, stripDelimiter, failFast);
    }

    public LineBasedFrameMessageDecoderAdapter(int maxLength, Class<? extends AbstractMessage> messageClass) {
        super(maxLength);
        this.messageClass = messageClass;
    }

    public LineBasedFrameMessageDecoderAdapter(int maxLength, boolean stripDelimiter
            , boolean failFast, Class<? extends AbstractMessage> messageClass) {
        super(maxLength, stripDelimiter, failFast);
        this.messageClass = messageClass;
    }

    @Override
    public Class<? extends AbstractMessage> getMessageClass() {
        return this.messageClass;
    }

    @Override
    public IotMessageDecoder setMessageClass(Class<? extends AbstractMessage> messageClass) {
        this.messageClass = messageClass;
        return this;
    }

    @Override
    public AbstractMessage decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        return this.proxy(ctx, (ByteBuf) super.decode(ctx, in));
    }
}
